jconed.hover = function ( editor, params )
{
	var self = this;
	this.params = params || {};
	
	this.editor = editor;
	var el = $( this.editor.id );

	/*
	var button = document.createElement ( 'span' );
	button.className = 'jconed-hover-button';
	*/
	var buttons = document.createElement ( 'div' );
	buttons.className = 'jconed-hover-buttons';

	var btnedit = document.createElement ( 'span' );
	btnedit.className = 'jconed-hover-button-edit';
	buttons.appendChild ( btnedit );

	var btnfull = document.createElement ( 'span' );
	btnfull.className = 'jconed-hover-button-editfull';
	buttons.appendChild ( btnfull );

	var btnlb = document.createElement ( 'span' );
	btnlb.className = 'jconed-hover-button-editlightbox';
	// DEBUG - buttons.appendChild ( btnlb );

	if ( this.params.get ( 'overlay' ) )
	{
		var overlay = document.createElement ( 'div' );
		overlay.className = 'jconed-hover-overlay';
		overlay.appendChild ( buttons );
		buttons.style.top = '10px';
		buttons.style.right = '10px';
		this.content = overlay;
		liwe.events.add ( this.content, 'mouseout', function ( ev ) { jconed.hover.prototype.mouseout.call ( self, ev ); } );
		liwe.events.add ( overlay, 'click', function ( ev ) { editor.do_edit ( jconed.editor.CURRENT ); } );
	}
	else
	{
		this.content = buttons;
		liwe.events.add ( el, 'mouseout', function ( ev ) { jconed.hover.prototype.mouseout.call ( self, ev ); } );
	}

	document.body.appendChild ( this.content );

	this.size = liwe.dom.get_size ( this.content );
	this.refresh ();
	this.hide ();
	
	liwe.events.add ( el, 'mouseover', function ( ev ) { jconed.hover.prototype.mouseover.call ( self, ev ); } );
	liwe.events.add ( btnedit, 'click', function ( ev ) { ev.stopPropagation(); editor.do_edit ( jconed.editor.INLINE ); } );
	liwe.events.add ( btnfull, 'click', function ( ev ) { ev.stopPropagation(); editor.do_edit ( jconed.editor.FULLSCREEN ); } );
	//liwe.events.add ( btnlb, 'click', function ( ev ) { ev.stopPropagation(); editor.do_edit ( jconed.editor.LIGHTBOX ); } );
};

jconed.hover.prototype.refresh = function ()
{
	var el = $( this.editor.id );
	var left = liwe.dom.get_offset_left ( el );
	var top = liwe.dom.get_offset_top ( el );
	var size = liwe.dom.get_size ( el );
	var s = this.content.style;

	if ( this.params.get ( 'overlay' ) )
	{
		var curr_style = liwe.dom.get_style ( el );
		var border_width = parseInt ( curr_style.borderLeftWidth, 10 ) + parseInt ( curr_style.borderRightWidth, 10 )
		var border_height = parseInt ( curr_style.borderTopWidth, 10 ) + parseInt ( curr_style.borderBottomWidth, 10 );
		s.left = left + 'px';
		s.top = top + 'px';
		s.width = ( size [ 0 ] + border_width ) + 'px';
		s.height = ( size [ 1 ] + border_height ) + 'px';
	}
	else
	{
		s.left = ( left + size [ 0 ] - this.size [ 0 ] - 10 ) + 'px';
		//s.top = ( top + size [ 1 ] - this.size [ 1 ] - 10 ) + 'px';
		s.top = ( top +  10 ) + 'px';
	}
};

jconed.hover.prototype.mouseover = function ( ev )
{
	if ( ! this.editor.enabled ) return true;
	this.refresh ();
	if ( ! this.editor.editing )
		this.show ();
};

jconed.hover.prototype.mouseout = function ( ev )
{
	if ( ev.relatedTarget == this.content ) return false;
	if ( liwe.dom.is_child_of ( this.editor.element, ev.relatedTarget ) ) return false;
	if ( liwe.dom.is_child_of ( this.content, ev.relatedTarget ) ) return false;

	this.hide ();
};

jconed.hover.prototype.show = function ()
{
	liwe.dom.show ( this.content );
};

jconed.hover.prototype.hide = function ()
{
	liwe.dom.hide ( this.content );
};
